import { defineStore } from 'pinia'
import axios from '@/axios'

export const useUserStore = defineStore('user', {
    state: () => ({
        user: {
            name: localStorage.getItem('name') || '',
            email: localStorage.getItem('email') || '',
            // 密码
            password: '',
            // 确认密码
            confirmPassword: '',
            // 上次登录时间戳
            lastTimeStamp:
                Number(localStorage.getItem(localStorage.getItem('email') + '-lastTimeStamp')) || 0,
            // 本次登录时间戳
            thisTimeStamp:
                Number(localStorage.getItem(localStorage.getItem('email') + '-thisTimeStamp')) || 0
        },
        // 标记是否已登录
        isLogged: !!localStorage.getItem('email')
    }),
    actions: {
        postUser(path: string) {
            return axios
                .post(path, {
                    name: this.user.name,
                    email: this.user.email,
                    password: this.user.password
                })
                .then(
                    (
                        res
                    ): {
                        timestamp: number
                        errMsg: string
                        data: { name: string; email: string }
                    } => {
                        // 开发环境下打印返回的数据
                        if (import.meta.env.DEV) console.log(res)
                        if (res.data.data) {
                            this.$patch({
                                // 更新user
                                user: {
                                    name: res.data.data.name,
                                    email: res.data.data.email,
                                    lastTimeStamp:
                                        Number(
                                            localStorage.getItem(
                                                res.data.data.email + '-thisTimeStamp'
                                            )
                                        ) || 0,
                                    thisTimeStamp: res.data.timestamp
                                },
                                // 标记已登录
                                isLogged: true
                            })
                            // 将name和email存入localStorage
                            localStorage.setItem('name', this.user.name)
                            localStorage.setItem('email', this.user.email)
                            // 将对应用户的timestamp存入localStorage
                            localStorage.setItem(
                                this.user.email + '-lastTimeStamp',
                                this.user.lastTimeStamp.toString()
                            )
                            localStorage.setItem(
                                this.user.email + '-thisTimeStamp',
                                this.user.thisTimeStamp.toString()
                            )
                        }
                        return res.data
                    }
                )
                .catch((err) => {
                    if (import.meta.env.DEV) console.log('catch axios error', err)
                    if (
                        err.code === 'ECONNABORTED' ||
                        err.code === 'ERR_NETWORK' ||
                        err.message.includes('timeout')
                    ) {
                        throw { code: 'timeout', message: '接口请求超时' }
                    }
                    throw err
                })
                .finally(() => {
                    // 清空密码
                    this.$patch({
                        user: {
                            password: '',
                            confirmPassword: ''
                        }
                    })
                })
        },
        logout() {
            if (import.meta.env.DEV) console.log('user start logout', this.user)
            localStorage.removeItem('name')
            localStorage.removeItem('email')
            this.$reset()
        }
    }
})
